package com.scoy.honeymei.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.CouponListAdapter;
import com.scoy.honeymei.adapter.VipHisAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.FilmHisBean;
import com.scoy.honeymei.bean.FilmListBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: VipHisActivity$
 * @Description: 观影会员记录
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 下午 14:06$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 下午 14:06$
 * @UpdateRemark: 更新内容
 */
public class VipHisActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private VipHisActivity mContext;
    private ArrayList<FilmHisBean.RecordBean> datalist;
    private VipHisAdapter vipHisAdapter;
    private int pageInt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_activity_list);
        ButterKnife.bind(this);
        mContext = VipHisActivity.this;

        initNormal();

        httpFilmhis(0);

    }

    @Override
    public void initNormal() {

        titleTv.setText(R.string.record);

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        datalist = new ArrayList<>();
        initRV();
        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                datalist.clear();
                httpFilmhis(0);

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);
                if(pageInt==-1){
                    MyUtil.mytoast(mContext,getString(R.string.nomore));
                }else {
                    httpFilmhis(pageInt);
                }
            }
        });

    }

    private void initRV() {
        vipHisAdapter = new VipHisAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
        normalRv.addItemDecoration(did);
        normalRv.setAdapter(vipHisAdapter);
        vipHisAdapter.setOnOneClick(new VipHisAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                MyUtil.mytoast0(mContext, "已成功领取", R.mipmap.ic_sure);
            }
        });

    }


    @OnClick({R.id.back_iv, R.id.title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_tv:
                break;
        }
    }

    private void httpFilmhis(int myPage) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(this, MyUrl.MOVIE_RECORD, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                FilmHisBean filmHisBean = new Gson().fromJson(o, FilmHisBean.class);
                int next = filmHisBean.getNext();
                List<FilmHisBean.RecordBean> record = filmHisBean.getRecord();
                pageInt = myPage + 1;
                if (next == 0) {
                    pageInt = -1;
                }
                datalist.addAll(record);
                vipHisAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
